﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NovoDataBase;
using NovoFileIO;
using Sunny.UI;

namespace NovoSerDes
{
    public partial class VPG : UIPage
    {
        List<RegisterConfig> configs = new List<RegisterConfig>();
        NovoDataSet DataSet;
        RegisterMap regMap;
        private uint hTotal = 0;
        private uint vTotal = 0;
        

        public VPG()
        {
            InitializeComponent();
            uiCheckBox_DE1.Checked = true;
            uiCheckBox_Hsync1.Checked = true; 
            uiCheckBox_Vsync1.Checked = true;
            uiCheckBox_Pat1.Checked = true;
            uiCheckBox_PCLKS1.Checked = true;
            uiComboBox_Part.SelectedIndex = 0;
            hTotal = (uint)(uiIntegerUpDown_pkt.Value + uiIntegerUpDown_hfp.Value + uiIntegerUpDown_hsa.Value + uiIntegerUpDown_hbp.Value);
            vTotal = (uint)(uiIntegerUpDown_vactive.Value + uiIntegerUpDown_vfp.Value + uiIntegerUpDown_vsa.Value + uiIntegerUpDown_vbp.Value);
        }

        Part curSeclectDevice = Part.NLS9246;
        private uint devAddr = 0x40;
        private uint mipiDev0_FirstAddr = 0xB080;
        private uint mipiDev1_FirstAddr = 0xB480;
        private uint mipiDev2_FirstAddr = 0xB880;
        private uint mipiDev3_FirstAddr = 0xBC80;

        private void uiAvatar1_Paint(object sender, PaintEventArgs e)
        {
            uiAvatar1.FillColor = Color.Transparent;
            Pen p = new Pen(Color.Black, 1);
            int SideLength = 15;
            e.Graphics.DrawLine(p, 0, SideLength, SideLength, SideLength);
            e.Graphics.DrawLine(p, SideLength, SideLength, SideLength, 0);
            e.Graphics.DrawLine(p, SideLength, 0, 2 * SideLength, 0);
            e.Graphics.DrawLine(p, 2 * SideLength, 0, 2 * SideLength, SideLength);
            e.Graphics.DrawLine(p, 2 * SideLength, SideLength, 3 * SideLength, SideLength);
        }

        private void uiAvatar2_Paint(object sender, PaintEventArgs e)
        {
            uiAvatar2.FillColor = Color.Transparent;
            Pen p = new Pen(Color.Black, 1);
            int SideLength = 15;
            e.Graphics.DrawLine(p, 0, 0, SideLength, 0);
            e.Graphics.DrawLine(p, SideLength, 0, SideLength, SideLength);
            e.Graphics.DrawLine(p, SideLength, SideLength, 2 * SideLength, SideLength);
            e.Graphics.DrawLine(p, 2 * SideLength, SideLength, 2 * SideLength, 0);
            e.Graphics.DrawLine(p, 2 * SideLength, 0, 3 * SideLength, 0);
        }

        private void uiCheckBox_DE1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_DE1.Checked)
            {
                uiCheckBox_DE2.Checked = false;
            }
            else if (!uiCheckBox_DE2.Checked)
            {
                uiCheckBox_DE1.Checked = true;
            }
        }

        private void uiCheckBox_DE2_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_DE2.Checked)
            {
                uiCheckBox_DE1.Checked = false;
            }
            else if (!uiCheckBox_DE1.Checked)
            {
                uiCheckBox_DE2.Checked = true;
            }
        }

        private void uiCheckBox_Hsync1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Hsync1.Checked)
            {
                uiCheckBox_Hsync2.Checked = false;
            }
            else if (!uiCheckBox_Hsync2.Checked)
            {
                uiCheckBox_Hsync1.Checked = true;
            }
        }

        private void uiCheckBox_Hsync2_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Hsync2.Checked)
            {
                uiCheckBox_Hsync1.Checked = false;
            }
            else if (!uiCheckBox_Hsync1.Checked)
            {
                uiCheckBox_Hsync2.Checked = true;
            }
        }

        private void uiCheckBox_Vsync1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Vsync1.Checked)
            {
                uiCheckBox_Vsync2.Checked = false;
            }
            else if (!uiCheckBox_Vsync2.Checked)
            {
                uiCheckBox_Vsync1.Checked = true;
            }
        }

        private void uiCheckBox_Vsync2_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Vsync2.Checked)
            {
                uiCheckBox_Vsync1.Checked = false;
            }
            else if (!uiCheckBox_Vsync1.Checked)
            {
                uiCheckBox_Vsync2.Checked = true;
            }
        }

        private void uiCheckBox_Pat1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Pat1.Checked)
            {
                uiCheckBox_Pat2.Checked = false;
            }
            else if (!uiCheckBox_Pat2.Checked)
            {
                uiCheckBox_Pat1.Checked = true;
            }
        }

        private void uiCheckBox_Pat2_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_Pat2.Checked)
            {
                uiCheckBox_Pat1.Checked = false;
            }
            else if (!uiCheckBox_Pat1.Checked)
            {
                uiCheckBox_Pat2.Checked = true;
            }

        }

        private void uiCheckBox_PCLKS1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_PCLKS1.Checked)
            {
                uiCheckBox_PCLKS2.Checked = false;
            }
            else if (!uiCheckBox_PCLKS2.Checked)
            {
                uiCheckBox_PCLKS1.Checked = true;
            }
        }

        private void uiCheckBox_PCLKS2_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_PCLKS2.Checked)
            {
                uiCheckBox_PCLKS1.Checked = false;
            }
            else if (!uiCheckBox_PCLKS1.Checked)
            {
                uiCheckBox_PCLKS2.Checked = true;
            }
        }

        enum Part
        {
            NLS9116 = 0,
            NLS9246 = 1
        }

        private void exportCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //OpenNsprojAndGetRegMap();

            string currentProjPath = "";

            if (curSeclectDevice == Part.NLS9246)
            {
                //文件名全匹配
                currentProjPath = System.IO.Path.Combine(Environment.CurrentDirectory, "nsproj file", "NLS9246_RegMap_MPW2_v0.2.nsproj");
                //文件名部分匹配
                //string searchRootPath = System.IO.Path.Combine(Environment.CurrentDirectory, "nsproj file");
                //string searchPattern = "*NLS9246*.nsproj";
                //string[] nsprojFilePaths = System.IO.Directory.GetFiles(searchRootPath, searchPattern, SearchOption.AllDirectories);
                //currentProjPath = nsprojFilePaths[0];

                if (!File.Exists(currentProjPath))
                {
                    UIMessageDialog.ShowMessageDialog("找不到NLS9246的nsproj文件", "提示", showCancelButton: false, UIStyle.Blue, showMask: false);
                    return;
                }
                DeserializeMethod(currentProjPath);
                regMap = DataSet.RegMap;
            }
            else if(curSeclectDevice == Part.NLS9116)
            {
                currentProjPath = System.IO.Path.Combine(Environment.CurrentDirectory, "nsproj file", "NLS9116A_RegMap_MPW2_v0.4.nsproj");
                if (!File.Exists(currentProjPath))
                {
                    UIMessageDialog.ShowMessageDialog("找不到NLS9116的nsproj文件", "提示", showCancelButton: false, UIStyle.Blue, showMask: false);
                    return;
                }
                DeserializeMethod(currentProjPath);
                regMap = DataSet.RegMap;
            }
            

            string cFilePath = CFileExporter.CreateCFilePath();
            System.Console.WriteLine(cFilePath);
            if (cFilePath != null)
            {
                CFileExporter CFile = new CFileExporter(cFilePath);

                if(curSeclectDevice == Part.NLS9246)
                {
                    Add9246Configs();
                }
                else if(curSeclectDevice == Part.NLS9116)
                {
                    Add9116Configs();
                }
                

                CFile.ExportCFile(configs);
            }
            else
            {
                System.Console.WriteLine("cFilePath is null");
                return;
            }
        }

        private void AddConfig(string comment, uint devaddr, uint regaddr, uint value)
        {
            RegisterConfig config = new RegisterConfig { Comment = comment + $" => Value: 0x{value:X8}", DevAddr = "0x" + devaddr.ToString("X"), RegAddr = "0x" + regaddr.ToString("X"), Value = "0x" + value.ToString("X8") };
            configs.Add(config);
        }

        private void DeserializeMethod(string path)
        {
            //创建一个文件流
            IFormatter formatter = new BinaryFormatter();
            Stream destream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            DataSet = formatter.Deserialize(destream) as NovoDataSet;
            destream.Close();
        }

        private void OpenNsprojAndGetRegMap()
        {
            #region 打开.nsproj获取DataSet和RegMap
            String currentProjPath = "";
            OpenFileDialog openProj = new OpenFileDialog();
            openProj.Title = "open an project file and update infomations to GUI...";
            openProj.Filter = "NSPROJ(.nsproj)|*.nsproj";

            if (openProj.ShowDialog() == DialogResult.OK)
            {
                currentProjPath = openProj.FileName;
                DeserializeMethod(currentProjPath);

                regMap = DataSet.RegMap;
            }
            #endregion
        }

        private void Add9246Configs()
        {
            configs.Clear();
            //MIPI_DEV0
            uint configValue = 0;
            UpdateConfigValue(ref configValue, (uint)1, 0, 0); //Vpg_en
            AddConfig(regMap[mipiDev0_FirstAddr].RegName, devAddr, mipiDev0_FirstAddr, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_dt.Value, 0, 5); //vpg_dt
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vc.Value, 6, 7);//vpg_vc
            UpdateConfigValue(ref configValue, (uint)0, 8, 8);//vpg_hsync_pkt_en
            UpdateConfigValue(ref configValue, (uint)1, 9, 10); //vpg_line_num_mode
            UpdateConfigValue(ref configValue, (uint)1, 11, 11); //vpg_frame_num_mode
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 12, 14);//vpg_vcx
            AddConfig(regMap[mipiDev0_FirstAddr + 3].RegName, devAddr, mipiDev0_FirstAddr + 3, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_pkt.Value, 0, 13);//1.2、pkt
            AddConfig(regMap[mipiDev0_FirstAddr + 4].RegName, devAddr, mipiDev0_FirstAddr + 4, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hsa.Value, 0, 11);//1.4、hsa
            AddConfig(regMap[mipiDev0_FirstAddr + 5].RegName, devAddr, mipiDev0_FirstAddr + 5, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hbp.Value, 0, 11); //1.1、hbp
            AddConfig(regMap[mipiDev0_FirstAddr + 6].RegName, devAddr, mipiDev0_FirstAddr + 6, configValue);
            configValue = 0;
            string stringValue = uiTextBox_hline.Text.Trim();
            if (uint.TryParse(stringValue, System.Globalization.NumberStyles.Number, null, out uint uintValue))
            {
                hTotal = uintValue;
                //Console.WriteLine("TryParse Success");
            }
            UpdateConfigValue(ref configValue, hTotal, 0, 14);//1.5、hline
            AddConfig(regMap[mipiDev0_FirstAddr + 7].RegName, devAddr, mipiDev0_FirstAddr + 7, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vsa.Value, 0, 9);//2.4、vsa
            AddConfig(regMap[mipiDev0_FirstAddr + 8].RegName, devAddr, mipiDev0_FirstAddr + 8, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 0, 9);//2.1、vbp
            AddConfig(regMap[mipiDev0_FirstAddr + 9].RegName, devAddr, mipiDev0_FirstAddr + 9, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vfp.Value, 0, 9);//2.3、vfp
            AddConfig(regMap[mipiDev0_FirstAddr + 0xA].RegName, devAddr, mipiDev0_FirstAddr + 0xA, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vactive.Value, 0, 13);//2.2、vactive
            AddConfig(regMap[mipiDev0_FirstAddr + 0xB].RegName, devAddr, mipiDev0_FirstAddr + 0xB, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_bk_lines.Value, 0, 9); //vpg_bk_lines
            AddConfig(regMap[mipiDev0_FirstAddr + 0xF].RegName, devAddr, mipiDev0_FirstAddr + 0xF, configValue);

            //MIPI_DEV1
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)1, 0, 0); //Vpg_en
            AddConfig(regMap[mipiDev1_FirstAddr].RegName, devAddr, mipiDev1_FirstAddr, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_dt.Value, 0, 5); //vpg_dt
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vc.Value, 6, 7);//vpg_vc
            UpdateConfigValue(ref configValue, (uint)0, 8, 8);//vpg_hsync_pkt_en
            UpdateConfigValue(ref configValue, (uint)1, 9, 10); //vpg_line_num_mode
            UpdateConfigValue(ref configValue, (uint)1, 11, 11); //vpg_frame_num_mode
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 12, 14);//vpg_vcx
            AddConfig(regMap[mipiDev1_FirstAddr + 3].RegName, devAddr, mipiDev1_FirstAddr + 3, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_pkt.Value, 0, 13);//1.2、pkt
            AddConfig(regMap[mipiDev1_FirstAddr + 4].RegName, devAddr, mipiDev1_FirstAddr + 4, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hsa.Value, 0, 11);//1.4、hsa
            AddConfig(regMap[mipiDev1_FirstAddr + 5].RegName, devAddr, mipiDev1_FirstAddr + 5, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hbp.Value, 0, 11); //1.1、hbp
            AddConfig(regMap[mipiDev1_FirstAddr + 6].RegName, devAddr, mipiDev1_FirstAddr + 6, configValue);
            configValue = 0;
            stringValue = uiTextBox_hline.Text.Trim();
            if (uint.TryParse(stringValue, System.Globalization.NumberStyles.Number, null, out uintValue))
            {
                hTotal = uintValue;
                //Console.WriteLine("TryParse Success");
            }
            UpdateConfigValue(ref configValue, hTotal, 0, 14);//1.5、hline
            AddConfig(regMap[mipiDev1_FirstAddr + 7].RegName, devAddr, mipiDev1_FirstAddr + 7, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vsa.Value, 0, 9);//2.4、vsa
            AddConfig(regMap[mipiDev1_FirstAddr + 8].RegName, devAddr, mipiDev1_FirstAddr + 8, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 0, 9);//2.1、vbp
            AddConfig(regMap[mipiDev1_FirstAddr + 9].RegName, devAddr, mipiDev1_FirstAddr + 9, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vfp.Value, 0, 9);//2.3、vfp
            AddConfig(regMap[mipiDev1_FirstAddr + 0xA].RegName, devAddr, mipiDev1_FirstAddr + 0xA, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vactive.Value, 0, 13);//2.2、vactive
            AddConfig(regMap[mipiDev1_FirstAddr + 0xB].RegName, devAddr, mipiDev1_FirstAddr + 0xB, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_bk_lines.Value, 0, 9); //vpg_bk_lines
            AddConfig(regMap[mipiDev1_FirstAddr + 0xF].RegName, devAddr, mipiDev1_FirstAddr + 0xF, configValue);

            //MIPI_DEV2
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)1, 0, 0); //Vpg_en
            AddConfig(regMap[mipiDev2_FirstAddr].RegName, devAddr, mipiDev2_FirstAddr, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_dt.Value, 0, 5); //vpg_dt
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vc.Value, 6, 7);//vpg_vc
            UpdateConfigValue(ref configValue, (uint)0, 8, 8);//vpg_hsync_pkt_en
            UpdateConfigValue(ref configValue, (uint)1, 9, 10); //vpg_line_num_mode
            UpdateConfigValue(ref configValue, (uint)1, 11, 11); //vpg_frame_num_mode
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 12, 14);//vpg_vcx
            AddConfig(regMap[mipiDev2_FirstAddr + 3].RegName, devAddr, mipiDev2_FirstAddr + 3, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_pkt.Value, 0, 13);//1.2、pkt
            AddConfig(regMap[mipiDev2_FirstAddr + 4].RegName, devAddr, mipiDev2_FirstAddr + 4, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hsa.Value, 0, 11);//1.4、hsa
            AddConfig(regMap[mipiDev2_FirstAddr + 5].RegName, devAddr, mipiDev2_FirstAddr + 5, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hbp.Value, 0, 11); //1.1、hbp
            AddConfig(regMap[mipiDev2_FirstAddr + 6].RegName, devAddr, mipiDev2_FirstAddr + 6, configValue);
            configValue = 0;
            stringValue = uiTextBox_hline.Text.Trim();
            if (uint.TryParse(stringValue, System.Globalization.NumberStyles.Number, null, out uintValue))
            {
                hTotal = uintValue;
                //Console.WriteLine("TryParse Success");
            }
            UpdateConfigValue(ref configValue, hTotal, 0, 14);//1.5、hline
            AddConfig(regMap[mipiDev2_FirstAddr + 7].RegName, devAddr, mipiDev2_FirstAddr + 7, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vsa.Value, 0, 9);//2.4、vsa
            AddConfig(regMap[mipiDev2_FirstAddr + 8].RegName, devAddr, mipiDev2_FirstAddr + 8, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 0, 9);//2.1、vbp
            AddConfig(regMap[mipiDev2_FirstAddr + 9].RegName, devAddr, mipiDev2_FirstAddr + 9, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vfp.Value, 0, 9);//2.3、vfp
            AddConfig(regMap[mipiDev2_FirstAddr + 0xA].RegName, devAddr, mipiDev2_FirstAddr + 0xA, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vactive.Value, 0, 13);//2.2、vactive
            AddConfig(regMap[mipiDev2_FirstAddr + 0xB].RegName, devAddr, mipiDev2_FirstAddr + 0xB, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_bk_lines.Value, 0, 9); //vpg_bk_lines
            AddConfig(regMap[mipiDev2_FirstAddr + 0xF].RegName, devAddr, mipiDev2_FirstAddr + 0xF, configValue);

            //MIPI_DEV3
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)1, 0, 0); //Vpg_en
            AddConfig(regMap[mipiDev3_FirstAddr].RegName, devAddr, mipiDev3_FirstAddr, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_dt.Value, 0, 5); //vpg_dt
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vc.Value, 6, 7);//vpg_vc
            UpdateConfigValue(ref configValue, (uint)0, 8, 8);//vpg_hsync_pkt_en
            UpdateConfigValue(ref configValue, (uint)1, 9, 10); //vpg_line_num_mode
            UpdateConfigValue(ref configValue, (uint)1, 11, 11); //vpg_frame_num_mode
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 12, 14);//vpg_vcx
            AddConfig(regMap[mipiDev3_FirstAddr + 3].RegName, devAddr, mipiDev3_FirstAddr + 3, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_pkt.Value, 0, 13);//1.2、pkt
            AddConfig(regMap[mipiDev3_FirstAddr + 4].RegName, devAddr, mipiDev3_FirstAddr + 4, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hsa.Value, 0, 11);//1.4、hsa
            AddConfig(regMap[mipiDev3_FirstAddr + 5].RegName, devAddr, mipiDev3_FirstAddr + 5, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hbp.Value, 0, 11); //1.1、hbp
            AddConfig(regMap[mipiDev3_FirstAddr + 6].RegName, devAddr, mipiDev3_FirstAddr + 6, configValue);
            configValue = 0;
            stringValue = uiTextBox_hline.Text.Trim();
            if (uint.TryParse(stringValue, System.Globalization.NumberStyles.Number, null, out uintValue))
            {
                hTotal = uintValue;
                //Console.WriteLine("TryParse Success");
            }
            UpdateConfigValue(ref configValue, hTotal, 0, 14);//1.5、hline
            AddConfig(regMap[mipiDev3_FirstAddr + 7].RegName, devAddr, mipiDev3_FirstAddr + 7, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vsa.Value, 0, 9);//2.4、vsa
            AddConfig(regMap[mipiDev3_FirstAddr + 8].RegName, devAddr, mipiDev3_FirstAddr + 8, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 0, 9);//2.1、vbp
            AddConfig(regMap[mipiDev3_FirstAddr + 9].RegName, devAddr, mipiDev3_FirstAddr + 9, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vfp.Value, 0, 9);//2.3、vfp
            AddConfig(regMap[mipiDev3_FirstAddr + 0xA].RegName, devAddr, mipiDev3_FirstAddr + 0xA, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vactive.Value, 0, 13);//2.2、vactive
            AddConfig(regMap[mipiDev3_FirstAddr + 0xB].RegName, devAddr, mipiDev3_FirstAddr + 0xB, configValue);
            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_bk_lines.Value, 0, 9); //vpg_bk_lines
            AddConfig(regMap[mipiDev3_FirstAddr + 0xF].RegName, devAddr, mipiDev3_FirstAddr + 0xF, configValue);
        }

        private void Add9116Configs()
        {
            configs.Clear();

            uint configValue = 0;    
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_bk_lines.Value, 22, 31); //vpg_bk_lines
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_dt.Value, 16, 21); //vpg_dt
            UpdateConfigValue(ref configValue, (uint)1, 15, 15); //Vpg_en
            UpdateConfigValue(ref configValue, (uint)0, 14, 14); //vpg_frame_num
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hbp.Value, 2, 13); //1.1、hbp
            AddConfig(regMap[0x002C].RegName, devAddr, 0x002C, configValue);

            configValue = 0;
            string stringValue = uiTextBox_hline.Text.Trim();
            if (uint.TryParse(stringValue, System.Globalization.NumberStyles.Number, null, out uint uintValue))
            {
                hTotal = uintValue;
                //Console.WriteLine("TryParse Success");
            }
            UpdateConfigValue(ref configValue, hTotal, 17, 31);//1.5、hline
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_hsa.Value, 5, 16);//1.4、hsa
            UpdateConfigValue(ref configValue, (uint)0, 4, 4);//vpg_hsync_packet_en
            UpdateConfigValue(ref configValue, (uint)0, 2, 3);//vpg_line_num
            AddConfig(regMap[0x0030].RegName, devAddr, 0x0030, configValue);

            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)0x53f0, 16, 31);//vpg_max_frame_num
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_mode.Value, 15, 15);//vpg_mode
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_orientation_qst.Value, 14, 14);//vpg_orientation_qst
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_pkt.Value, 0, 13);//1.2、pkt
            AddConfig(regMap[0x0034].RegName, devAddr, 0x0034, configValue);

            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)0xb1f, 16, 31);//vpg_start_line_num
            UpdateConfigValue(ref configValue, (uint)0x634, 0, 15);//vpg_step_line_num
            AddConfig(regMap[0x0038].RegName, devAddr, 0x0038, configValue);

            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vactive.Value, 18, 31);//2.2、vactive
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vbp.Value, 8, 17);//2.1、vbp
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vc.Value, 6, 7);//vpg_vc
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vcx.Value, 3, 5);//vpg_vcx
            AddConfig(regMap[0x003C].RegName, devAddr, 0x003C, configValue);

            configValue = 0;
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vfp.Value, 22, 31);//2.3、vfp
            UpdateConfigValue(ref configValue, (uint)uiIntegerUpDown_vsa.Value, 12, 21);//2.4、vsa
            AddConfig(regMap[0x0040].RegName, devAddr, 0x0040, configValue);
        }

        private void uiComboBox_Part_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (uiComboBox_Part.SelectedIndex)
            {
                case 0:
                    curSeclectDevice = Part.NLS9116;
                    devAddr = 0x40;
                    break;
                case 1:
                    curSeclectDevice = Part.NLS9246;
                    devAddr = 0x29;
                    break;
            }
        }

        //ref关键字用于声明一个参数是按引用传递的
        private void UpdateConfigValue(ref uint configValue, uint bfvalue, uint startBit, uint endBit)
        {
            //uint bfMask = ((1u << ((int)endBit + 1)) - 1) & ~((1u << (int)startBit) - 1);
            UInt32 bfMask = (UInt32)(Math.Pow(2, endBit + 1) - Math.Pow(2, startBit));
            uint maskedValue = (bfvalue << (int)startBit) & bfMask;
            configValue = (configValue & ~bfMask) | maskedValue;
        }

        private void uiButton_CalFPS_Click(object sender, EventArgs e)
        {
            hTotal = (uint)(uiIntegerUpDown_pkt.Value + uiIntegerUpDown_hfp.Value + uiIntegerUpDown_hsa.Value + uiIntegerUpDown_hbp.Value);
            vTotal = (uint)(uiIntegerUpDown_vactive.Value + uiIntegerUpDown_vfp.Value + uiIntegerUpDown_vsa.Value + uiIntegerUpDown_vbp.Value);
            uiTextBox_hline.Text = hTotal.ToString();
            uiTextBox_vline.Text = vTotal.ToString();
            uiIntegerUpDown_bk_lines.Value = uiIntegerUpDown_hfp.Value + uiIntegerUpDown_hsa.Value + uiIntegerUpDown_hbp.Value;
        }
    }
}
